<?php

class	dbManipulator
{
	private static $_pdoInstance;
	private static $_instance;
	private $_request;
	private $_args = NULL;
	private $_wherecalled = false;

	/* Nobody can create an instance from dbManipulator */
	private function __construct() {} 
	private function __clone() {}
	
	/*
	* @return a static instance of dbManipulator
	* @acces public
	* @param $pdo, $host, $user, $password, $db
	* @return dbManipulator instance
	*/
	public static function getInstance($pdo, $host, $user, $password, $db)
	{
		if (!self::$_instance)
		{
			self::$_pdoInstance = new PDO($pdo.':host='.$host.';dbname='.$db, $user, $password);
			self::$_pdoInstance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$c = __CLASS__;
			self::$_instance = new $c;
		}
		return self::$_instance;
	}
	
	/*
	* @Prepare the PDO request
	* @acces private
	* @param $query
	* @return void
	*/
	private function Prepare($query)
	{
		self::$_instance->_request = self::$_pdoInstance->prepare($query);
		self::$_instance->_request->execute();
	}
	
	/*
	* @Return the fetch query
	* @acces pulic
	* @param $query
	* @return array
	*/
	public function Fetch($query)
	{
		self::$_instance->Prepare($query);
		return self::$_instance->_request->fetchAll(PDO::FETCH_ASSOC);
	}
	
	/*
	* @Check for a match
	* @acces public
	* @param $var, $tableName
	* @return int
	*/
	public function Match($var = array(), $tableName)
	{
		$req = NULL;
		$i = 1;
		foreach ($var as $key => $value) {
			$req .= "$key = '$value'";
			if ($i != count($var)) {
				$req .= " AND ";
			}
			$i++;
		}
		self::$_instance->Fetch("SELECT * FROM $tableName WHERE $req");
		return self::$_instance->_request->rowCount();
	}
	
	/*
	* @Add a FROM clause to the query
	* @acces public
	* @param $dbName, $select
	* @return void
	*/
	public function from($dbName, $select)
	{
		self::$_instance->_args .= "SELECT $select FROM $dbName";
	}
	
	/*
	* @Add a WHERE clause to the query
	* @acces public
	* @param $arg
	* @return void
	*/
	public function where($arg)
	{
		if (self::$_instance->_wherecalled == false) {
			self::$_instance->_args .= " WHERE $arg";
			self::$_instance->_wherecalled = true;
		} else {
			self::$_instance->_args .= " and $arg";
		}
	}
	
	/*
	* @Add a OR clause to the query
	* @acces public
	* @param $arg
	* @return void
	*/
	public function orwhere($arg)
	{
		self::$_instance->_args .= " OR $arg";
	}
	
	/*
	* @Add an instance of dbManipulator
	* @acces public
	* @return instance
	*/
	public function select()
	{
		return self::$_instance;
	}
	
	/*
	* @Add GROUP BY clause to the query
	* @acces public
	* @param $arg
	* @return void
	*/
	public function group($arg)
	{
		self::$_instance->_args .= " GROUP BY $arg";
	}
	
	/*
	* @Add ORDER BY clause to the query
	* @acces public
	* @param $arg
	* @return void
	*/
	public function order($arg)
	{
		self::$_instance->_args .= " ORDER BY $arg";
	}
	
	/*
	* @return the request _args
	* @acces public
	* @return string
	*/
	public function getRequest()
	{
		return self::$_instance->_args;
	}
	
	/*
	* @execute the SQL query from _args
	* @acces public
	* @return array
	*/
	public function executeQuery()
	{
		$ret = self::$_instance->Fetch(self::$_instance->_args);
		self::$_instance->_args = NULL;
		return $ret;
	}
	
	public function Insert($table, $row, $value)
	{
		self::$_instance->Prepare("INSERT INTO $table ($row) VALUES ('$value')");
	}
	
	public function Delete($table, $var, $value)
	{
		self::$_instance->Prepare("DELETE FROM $table WHERE $var = $value");
	}
	
	public function ManualQuery($query)
	{
		self::$_instance->Prepare($query);
	}
	
}

?>